<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <!--登录表单-->
      <el-form
        :model="loginForm"
        :rules="loginFormRule"
        ref="loginFormRef"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button @click="resetLoginForm" type="info">重置</el-button>
          <el-button @click="login" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单
      loginForm: {
        username: '',
        password: ''
      },
      // 表单验证规则
      loginFormRule: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        const response = await this.$request.post('login', this.loginForm)
        this.$message({
          message: response.meta.msg || '成功',
          type: 'success'
        })
        const tokenStr = response.data.token
        // 存储 token
        if (tokenStr) {
          window.sessionStorage.setItem('token', tokenStr)
        }
        this.$router.push('/home')
      })
    },
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;

    .avatar_box {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      padding: 3px;
      background-color: #fff;
      border-radius: 50%;
      border: 1px solid #eee;

      img {
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .login_form {
      box-sizing: border-box;
      padding: 0 20px;
      width: 100%;
      position: absolute;
      bottom: 0;

      .buttons {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
